package proyecto.dao;

import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Censura;
import proyecto.modelo.Departamento;
import proyecto.modelo.DetCartelera;

public class DetCarteleraDAOImpl extends HibernateDaoSupport implements DetCarteleraDAO {

	protected Log log = LogFactory.getLog(getClass());

	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	@SuppressWarnings("unchecked")
	public List<DetCartelera> listar() throws DAOExcepcion {
		log.info("listar()");
		List<DetCartelera> lista = new ArrayList<DetCartelera>();
		try {
			lista = getHibernateTemplate().find("from DetCartelera");
		} catch (DataAccessException e) {
			log.error(e.toString());
			throw new DAOExcepcion(e.getMessage());
		}
		return lista;
	}

	public DetCartelera obtener(int coDetcartelera) throws DAOExcepcion {
		log.info("obtener() : " + coDetcartelera);
		DetCartelera modelo = new DetCartelera();
		try {
			modelo = (DetCartelera) getHibernateTemplate().find(
					"from DetCartelera a where trim(a.coDetcartelera) = ?",
					coDetcartelera + "").get(0);
		} catch (Exception e) {
			log.error("Error: " + e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		return modelo;
	}
	
	public DetCartelera actualizar(DetCartelera modelo) throws DAOExcepcion {
		try {
			getHibernateTemplate().update(modelo);
			getHibernateTemplate().flush();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		return modelo;
	}
	
	public DetCartelera insertar(DetCartelera modelo) throws DAOExcepcion {
		try {
			getHibernateTemplate().save(modelo);
			getHibernateTemplate().flush();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		return modelo;
	}
	
	public void eliminar(int coDetcartelera) throws DAOExcepcion {
		DetCartelera modelo = new DetCartelera();
		modelo.setCoDetcartelera(coDetcartelera);
		try {
			getHibernateTemplate().delete(modelo);
			getHibernateTemplate().flush();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
	}
}
